//检查是不是管理员
$(function(){
	var ayadmin=localStorage.getItem("ayadmin");
	if(ayadmin!="taisadmin"){
		window.location.href="./admin.html";
	}
})

//退出与查询管理员列表
$(function(){
	$("#manage").click(function(){
		var top=$("#admin").css("top");
		if(top=="0px"){
			$("#admin").css({"top":"40px"});	
		}else{
			$("#admin").css({"top":"0px"});
		}
		$("#removeBut").click();
	});
});

$(function(){
	$("#content").click(function(){
		$("#admin").css({"top":"0px"});
		$("#removeBut").click();
	})
});

//查看留言
$(function(){
	show();
});

//退出
$(function(){
	$("#back").click(function(){
		localStorage.setItem("ayadmin","taisnotadmin");
		window.location.href="./admin.html";
	})
})

var id,rd,ed;
//删除建言
function deleteSugg(event){
	$("#removeAdd").click();
	$("#noo").click();
	$("#aysure").show(500);
	event.stopPropagation();
	id=$(event.target).parents(".row").attr("id");
}

$(function(){
	$("#no").click(function(){
		$("#aysure").hide(500);
	});
	$("#yes").click(function(){
		$.ajax({
			type:"GET",
			url:"http://pads.nenu.edu.cn:8080/suggestions/admin/page/teacher/deleteSugg.php?suggid="+id,
			dataType:"json",
			success:function(x,y,z){
				if(x.key=="success"){
					window.location.reload(false);
				}else{
					$("#no").click();
					alert("删除失败！");
				}
			},
			error:function(jqXHR){
			    $("#no").click();
			    alert("发生错误："+jqXHR);;
			}
		});
	});
});
//删除回复
function deleteResp(event){
	$("#no").click();
	$("#removeAdd").click();
	$("#makeSure").show(500);
	event.stopPropagation();
	ed=$(event.target).attr("id");
}

$(function(){
	$("#noo").click(function(){
		$("#makeSure").hide(500);
	})
	$("#yeah").click(function(){
		$.ajax({
			type:"GET",
			url:"http://pads.nenu.edu.cn:8080/suggestions/admin/page/teacher/deleteResp.php?respid="+ed,
			dataType:"json",
			success:function(x,y,z){
				if(x.key=="success"){
					window.location.reload(false);
				}else{
					$("#noo").click();
					alert("删除失败！");
				}
			},
			error:function(jqXHR){
			    $("#noo").click();
			    alert("发生错误："+jqXHR);
			}
		});
	});
})
//添加回复
function addResp(event){
	$("#no").click();
	$("#noo").click();
	$("#suggadd").show(500);
	$("#suggCont").val("");
	$("#suggCont").focus();
	event.stopPropagation();
	rd=$(event.target).parents(".row").attr("id");
}

$(function(){
	$("#suggGo").click(function(){
		var suggCont=$("#suggCont").val()
		if(suggCont.length==0){
			$("#ques").html("请输入建言");
		}else{
			$.ajax({
				type:"POST",
				url:"http://pads.nenu.edu.cn:8080/suggestions/admin/page/teacher/respadd.php",
				dataType:"json",
				data:{
					respcontent:suggCont,
					suggid:rd
				},
				success:function(x,y,z){
					if(x.key=="success"){
						window.location.reload(false);
					}else{
						$("#removeAdd").click();
						alert("删除失败！");
					}
				},
				error:function(jqXHR){
				    $("#removeAdd").click();
				    alert("发生错误："+jqXHR);
				}
			});
		}
	});
});


$(function(){
	$("#removeAdd").click(function(){
		$("#suggadd").hide(500);
		$("#suggCont").val("");
		$("#ques").html("请填写回复信息");
	})
});

$(function(){
	$("#content").click(function(){
		$("#removeAdd").click();
		$("#no").click();
		$("#noo").click();
		$("#none").click();
		$("#removePass").click();
	})
})

//查看管理员
$(function(){
	$("#showAdmin").click(function(){
		admin();
	});
});

//显示建言及回复
function show(){
	$.ajax({
		type:"GET",
		url:"http://pads.nenu.edu.cn:8080/suggestions/admin/page/selectAll.php",
		dataType:"json",
		success:function(x,y,z){
			var content=$("#content");
			var overHead=$("#overHead");
			content.empty();
			overHead.empty();
			console.log(x)
			if(x.sugg.length==1){
				content.append("<h3 id='noSugg'>暂无建言</h3>");
			}else{
				content.append("<div class='container'></div>");
				overHead.append("<div class='container'></div>");
				for(var n=1;n<x.sugg.length;n++){
					content.children(".container").append(
						"<div class='row theCont' id='"+x.sugg[n].sugg_id+"'>"+
							"<div class='col-xs-12'>"+
								"<div class='remove'>"+
									"<button class='btn btn-default' onclick='deleteSugg(event)'><span class='glyphicon glyphicon-remove'></span></button>"+
									"<button class='btn btn-default' onclick='addResp(event)'><span class='glyphicon glyphicon-plus'></span></button>"+
								"</div>"+
								"<div class='proAcon row'>"+
									"<div class='propic col-xs-2'>"+"<img src='../image/"+x.sugg[n].sugg_propic+".png'>"+"</div>"+
									"<div class='cont col-xs-10'>"+x.sugg[n].sugg_content+"</div>"+
								"</div>"+
								"<div>"+
									"<div class='sugg_date'>"+x.sugg[n].sugg_date+"</div>"+
								"</div>"+
							"</div>"+
						"</div>"
					)
				};
				for(var m=1;m<x.resp.length;m++){
					var target=$("#content .container").find("#"+x.resp[m].resp_suggid);
					target.append(
						"<div class='col-xs-2'></div>"+
						"<div class='col-xs-10'>"+
							"<div class='remove'>"+
								"<button class='btn btn-default' onclick='deleteResp(event)'><span class='glyphicon glyphicon-remove' id='"+x.resp[m].resp_id+"'></span></button>"+
							"</div>"+
							"<div class='row'>"+
								"<div class='propica col-xs-2'>"+"<img src='../image/bwc.jpg'>"+"</div>"+
								"<div class='conta col-xs-10'>"+x.resp[m].resp_content+"</div>"+
							"</div>"+
							"<div>"+
								"<div class='sugg_date'>"+x.resp[m].resp_date+"</div>"+
							"</div>"+
						"</div>"
					);
					target.appendTo($("#overHead .container"));
				}
			}
		},
		error:function(jqXHR){
		    console.log("发生错误"+jqXHR.status);
		}
	});
};

function back(){
	localStorage.setItem("ayadmin","taisnotadmin");
	window.location.href="./admin.html";
}

function sugge(){
	show();
	var operation=$("#operation");
	operation.empty();
	operation.append(
		"<div class='col-xs-6' onclick='back()'>"+
			"退&nbsp;&nbsp;&nbsp;&nbsp;出"+
		"</div>"+
		"<div class='col-xs-6' onclick='admin()'>"+
			"查看管理员"+
		"</div>"
	);
	$("#manage").click();
};

function admin(){
	$("#none").click();
	$.ajax({
		type:"GET",
		url:"http://pads.nenu.edu.cn:8080/suggestions/admin/page/teacher/admin/selectUser.php",
		dataType:"json",
		success:function(x,y,z){
			var name=localStorage.getItem("Username");
			var content=$("#content");
			content.empty();
			content.append("<div class='container'></div>");
			for(var n=0;n<x.length;n++){
				if(name=="admin"){
					if(x[n].user_rant!="root"){
						content.children(".container").append(
							"<div class='row adminLie'>"+
								"<div class='col-xs-5'>"+
									"账号："+x[n].user_account+
								"</div>"+
								"<div class='col-xs-5'>"+
									"权限："+"普通管理员"+
								"</div>"+
								"<div class='col-xs-2'>"+
									"<button class='btn btn-default' onclick='removAdmin(event)'>"+
										"<span class='glyphicon glyphicon-remove' id='"+x[n].user_id+"'></span>"+
									"</button>"+
								"</div>"+
							"</div>"
						);
					}else{
						content.children(".container").append(
							"<div class='row'>"+
								"<div class='col-xs-5'>"+
									"账号："+x[n].user_account+
								"</div>"+
								"<div class='col-xs-5'>"+
									"权限："+"超级管理员"+
								"</div>"+
								"<div class='col-xs-2'>"+
								"</div>"+
							"</div>"
						);
					}
				}else{
					if(x[n].user_rant!="root"){
						content.children(".container").append(
							"<div class='row adminLie'>"+
								"<div class='col-xs-5'>"+
									"账号："+x[n].user_account+
								"</div>"+
								"<div class='col-xs-5'>"+
									"权限："+"普通管理员"+
								"</div>"+
								"<div class='col-xs-2'>"+
								"</div>"+
							"</div>"
						);
					}else{
						content.children(".container").append(
							"<div class='row'>"+
								"<div class='col-xs-5'>"+
									"账号："+x[n].user_account+
								"</div>"+
								"<div class='col-xs-5'>"+
									"权限："+"超级管理员"+
								"</div>"+
								"<div class='col-xs-2'>"+
								"</div>"+
							"</div>"
						);
					}
				}
			};
			content.children(".container").append(
				"<div class='row'>"+
					"<div class='col-xs-12 addAdmin'>"+
						"<button type='button' class='btn btn-primary' onclick='addAdmin(event)'>添加管理员</button>"+
					"</div>"+
					"<div class='col-xs-12 addAdmin'>"+
						"<button type='button' class='btn btn-info' onclick='changeAdmin(event)'>修改我的密码</button>"+
					"</div>"+
				"</div>"
			);
			$("#manage").click();
			var operation=$("#operation");
			operation.empty();
			operation.append(
				"<div class='col-xs-6' onclick='back()'>"+
					"退&nbsp;&nbsp;&nbsp;&nbsp;出"+
				"</div>"+
				"<div class='col-xs-6' onclick='sugge()'>"+
					"显示建言"+
				"</div>"
			);
		},
		error:function(jqXHR){
		    $("#removeAdd").click();
		    alert("发生错误："+jqXHR);
		}
	});
}

function addAdmin(event){
	$("#logIn").show(500);
	event.stopPropagation();
}

$("#removeBut").click(function(event){
	$("#logIn").hide(500);
});

$(function(){
	$("#runGo").click(function(){
		var Username=$("#Username").val();
		var Password=$("#Password").val();;
		var feeback=$("#feedback");
		if(Username==""){
			feeback.html("请输入用户名！");
		}else if(Password==""){
			feeback.html("请输入密码！");
		}else{
			$.ajax({
				type:"POST",
				url:"http://pads.nenu.edu.cn:8080/suggestions/admin/page/selectUserAccount.php",
				dataType:"json",
				data:{
					account:Username
				},
				success:function(x,y,z){
					if(x.key=="ok"){
						$.ajax({
							type:"POST",
							url:"http://pads.nenu.edu.cn:8080/suggestions/admin/page/teacher/admin/access.php",
							dataType:"json",
							data:{
								account:Username,
								psw:Password
							},
							success:function(x,y,z){
								feeback.html("添加成功！");
								setTimeout(function(){
									admin();
									$("#manage").click();
								},500);
							},
							error:function(jqXHR){
							    console.log("发生错误"+jqXHR.status);
							}
						});
					}else{
						feeback.html("用户名已存在！");
					}
				},
				error:function(jqXHR){
				    console.log("发生错误"+jqXHR.status);
				}
			});
		}
	});
});
//删除管理员
var userId;
function removAdmin(event){
	userId=$(event.target).attr("id");
	$("#removeBut").click();
	$("#userSure").show(500);
	event.stopPropagation();
}

$(function(){
	$("#none").click(function(){
		$("#userSure").hide(500);
	});
	$("#aha").click(function(){
		$.ajax({
			type:"GET",
			url:"http://pads.nenu.edu.cn:8080/suggestions/admin/page/teacher/admin/deleteUser.php?userid="+userId,
			dataType:"json",
			success:function(x,y,z){
				if(x.key=="success"){
					admin();
					$("#manage").click();
				}
			},
			error:function(jqXHR){
			    console.log("发生错误"+jqXHR.status);
			}
		});
	});
})

//修改密码
function changeAdmin(event){
	$("#changePass").show(500);
	event.stopPropagation();
}

$(function(){
	$("#removePass").click(function(){
		$("#passOk").html("");
		$("#newPass").val("");
		$("#changePass").hide(500);
	});
});

$(function(){
	$("#passGo").click(function(){
		var newPass=$("#newPass").val();
		var passOk=$("#passOk");
		var name=localStorage.getItem("Username");
		if(newPass==""){
			passOk.html("请输入密码！")
		}else{
			$.ajax({
				type:"POST",
				url:"http://pads.nenu.edu.cn:8080/suggestions/admin/page/teacher/updateUser.php",
				dataType:"json",
				data:{
					account:name,
					newpsw:newPass
				},
				success:function(x,y,z){
					if(x.key=="success"){
						passOk.val("修改成功！");
						setTimeout(function(){
							admin();
							$("#removePass").click();
							$("#manage").click();
						},500);
					}
				},
				error:function(jqXHR){
				    console.log("发生错误"+jqXHR.status);
				}
			});
		}
	});
});